package com.example.administrator.zhongyichou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.administrator.zhongyichou.R;
import com.example.administrator.zhongyichou.help.ImageLoader;
import com.example.administrator.zhongyichou.help.StringImageToList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/11/20.
 * 描述：
 */

public class LaunchDiseaseGridAdapter1 extends ListBaseAdapter {

    private OnClickListener onClickListener;
    private List<String> listPhotoUrl;

    public LaunchDiseaseGridAdapter1(Context mContext, List list, List<String> listPhotoUrl) {
        super(mContext, list);
        this.listPhotoUrl = listPhotoUrl;
    }

    @Override
    public int getCount() {
        return 1 + getList().size() + listPhotoUrl.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.launch_disease_grid_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            ImageLoader.centerCropRes(getContext(), R.mipmap.img_btn_add, holder.imgContent);
            holder.imgClose.setVisibility(View.GONE);
        } else if (position < listPhotoUrl.size() + 1) {
            ImageLoader.centerCropUrl(getContext(), StringImageToList.toImage(listPhotoUrl.get(position - 1)), holder.imgContent);
            holder.imgClose.setVisibility(View.VISIBLE);
            holder.imgClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onItemClick(position - 1, 0);
                }
            });
        } else {
            ImageLoader.centerCropUrl(getContext(), (String) getItem(position - 1 - listPhotoUrl.size()), holder.imgContent);
            holder.imgClose.setVisibility(View.VISIBLE);
            holder.imgClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onItemClick(position - 1 - listPhotoUrl.size(), 1);
                }
            });
        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.img_content)
        ImageView imgContent;
        @BindView(R.id.img_close)
        ImageView imgClose;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnClickListener {
        void onItemClick(int position, int type);
    }

    public void setOnClick(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
